package cn.bigcontainer.alt.meta.extension.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.bigcontainer.alt.common.entity.BigMap;
import cn.bigcontainer.alt.common.util.WoKit;
import cn.bigcontainer.alt.common.util.WoVelocityKit;
import cn.bigcontainer.alt.meta.dto.MObjectDto;
import cn.bigcontainer.alt.meta.dto.MViewDto;
import cn.bigcontainer.alt.meta.extension.MListDataFilter;

/**
 * 从request中获取velocity模板，并解析为list的where子句
 * @author jiaod
 *
 */
@Component(MListDataFilter.DEFAULT)
public class VelocityListFilter implements MListDataFilter {

	@Resource
	private WoVelocityKit velocityKit;
	
	@Override
	public String addWhere(MViewDto list, MViewDto search) {
		BigMap<String,Object> request = list.getApp().getObject().getRequest();
		String filterKey = MObjectDto.PREFIX_LIST_FILTER + list.getId();
		String filter = new BigMap<String, Object>().bigPutAll(request).bigPutAll(list.getData()).getString(filterKey);
		if (WoKit.isEmpty(filter)) {
			filterKey = MObjectDto.PREFIX_LIST_FILTER + list.getApp().getId();
			filter = new BigMap<String, Object>().bigPutAll(request).bigPutAll(list.getApp().getData()).getString(filterKey);
			if (WoKit.isEmpty(filter)) {
				filterKey = MObjectDto.PREFIX_LIST_FILTER + list.getApp().getObjectId();
				filter = new BigMap<String, Object>().bigPutAll(request).bigPutAll(list.getApp().getObject().getData()).getString(filterKey);
			}
		}
		if (WoKit.isEmpty(filter)) {
			return null;
		}
		request.remove(filterKey);
		return velocityKit.parse(filter, request);
	}

	@Override
	public boolean replace() {
		return false;
	}

}
